package diandiansong.library.widgets;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import diandiansong.library.R;
import diandiansong.library.tabviewpager.utilsv4.FragmentPagerItemAdapter;
import diandiansong.library.tabviewpager.utilsv4.FragmentPagerItems;

public class SmartTabView extends RelativeLayout {

    FlowLayout flowLayout;
    public ViewPager mViewPager;
    MagicIndicator viewPagerTab;
    View mRlDropView;

    public SmartTabView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.smarttabview, this);

        flowLayout = findViewById(R.id.fl_all);
        mViewPager = findViewById(R.id.viewpager);
        viewPagerTab = findViewById(R.id.viewpagertab);
        mRlDropView = findViewById(R.id.rl_drop_view);
    }

    public void init(Context context, FragmentManager fm, final String[] titles, Class<? extends Fragment>[] frgments, Bundle[] bundles) {


        FragmentPagerItems.Creator creator = FragmentPagerItems.with(context);

        for (int i = 0; i < titles.length; ++i) {
            creator.add(titles[i], frgments[i], bundles[i]);

            View itemView = LayoutInflater.from(context).inflate(R.layout.smarttabview_item, null);
            itemView.setLayoutParams(new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
                    .LayoutParams.WRAP_CONTENT)));
            TextView tvName = itemView.findViewById(R.id.tv_name);
            tvName.setText(titles[i]);
            itemView.setTag(i);
            itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (Integer) v.getTag();
                    mRlDropView.setVisibility(INVISIBLE);
                    mViewPager.setCurrentItem(position);

                }
            });

            flowLayout.addView(itemView);
        }

        FragmentPagerItems pagerItems = creator.create();

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(fm, pagerItems);


        mViewPager.setAdapter(adapter);

        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(Color.GRAY);
                colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.colorPrimary));
                colorTransitionPagerTitleView.setSelectedColor(Color.BLACK);
                colorTransitionPagerTitleView.setText(titles[index]);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(getResources().getColor(R.color.colorPrimary));
                return indicator;
            }
        });
        viewPagerTab.setNavigator(commonNavigator);
        ViewPagerHelper.bind(viewPagerTab, mViewPager);

        ImageView more = findViewById(R.id.iv_show_all_tab);
        more.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int visibility = mRlDropView.getVisibility();
                mRlDropView.setVisibility(visibility == VISIBLE ? INVISIBLE : VISIBLE);
            }
        });
    }
}
